import time
import unittest

from _init_ import TestCaseOne
from selenium import webdriver
from selenium.webdriver.common.by import By
# from unittest import TestCase
from ddt import ddt, data, unpack
from method import method

da1 = [
    ['lisi', 123456, 10000, 10]
]
da2 = da1
da3 = da2
da4 = [
    ['lisi', 123456, 100000000000000000, 10]
]
da5 = [
    ['lisi', 123456, 10, 10]
]
da6 = [
    ['lisi', 123456, 100, 366]
]
da7 = [
    ['lisi', 123456, 100, 365]
]


# 选择跳过的方法
def chooseSkip(get_skip_or_no_skip, get_choose_case):
    all_test = []
    for i in range(1, 8):
        all_test.append(i)
    for j in get_choose_case:
        all_test.remove(j)
    if get_skip_or_no_skip == 1:
        the_choose_skip = all_test
    else:
        the_choose_skip = get_choose_case
    return the_choose_skip


# 选择跳过所选用例则选择0，选择执行所选用例则选择1
skip_or_no_skip = 0
choose_case = []
choose_skip = chooseSkip(skip_or_no_skip, choose_case)

s = method()
# 创建一个列表用来存储选择，有多少方法就准备多少元素
skip_test = s.skipTest(7, choose_skip)
e = 1234567890123456


@ddt
class TestBankcard(TestCaseOne):
    @unittest.skipIf(skip_test[0] == 1, "暂时跳过")
    @data(*da1)
    @unpack
    def test_001_applySuccess(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击个人中心
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-account-location']").click()
        time.sleep(1)

        # 点击我的借贷
        driver.find_element(By.LINK_TEXT, "我的借贷").click()
        time.sleep(1)

        # 获取借贷记录
        trs = driver.find_elements(By.TAG_NAME, "tr")
        if len(trs) == 0:
            old_log = "0条"
        else:
            old_log = driver.find_element(By.CSS_SELECTOR,
                                          "body > div > div > main > div > div > div > div > div.card-body > div.row > div:nth-child(2) > div > button:nth-child(7) > span").text

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(1)

        #  获取实际标题结果
        result = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text

        time.sleep(3)

        # 点击个人中心
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-account-location']").click()
        time.sleep(1)

        # 点击我的借贷
        driver.find_element(By.LINK_TEXT, "我的借贷").click()
        time.sleep(1)

        # 获取借贷记录
        new_log = driver.find_element(By.CSS_SELECTOR,
                                      "body > div > div > main > div > div > div > div > div.card-body > div.row > div:nth-child(2) > div > button:nth-child(7) > span").text
        old_log_num = old_log[0]
        new_log_num = new_log[0]
        flag = True
        flag2 = True
        # 判断借贷信息有没有增加一条
        if int(new_log_num) != (int(old_log_num) + 1):
            flag2 = False
        if "申请成功!" in result and flag2 == True:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_001_applySuccess")
            driver.save_screenshot(screenshot)
            flag = False

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertEqual(True, flag)

    @unittest.skipIf(skip_test[1] == 1, "暂时跳过")
    @data(*da2)
    @unpack
    def test_002_addlyPass(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(3)

        driver.get("http://localhost:90/")

        # 管理员常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys('admin')

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys('123456')

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(2)

        # 点击网贷管理
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[5]/a/i").click()
        time.sleep(1)

        # 点击网贷审核
        driver.find_element(By.LINK_TEXT, "网贷审核").click()
        time.sleep(1)

        result = ""
        # 点击通过审核（因为用户是新创建的，所以正常情况下只会有一条网贷申请）
        trs = driver.find_elements(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td")
        if len(trs) != 8:
            result = "管理员端未获得网贷信息"
            print("用例不通过，", result)
            screenshot = s.failedScreenshotNamed("test_002_addlyPass")
            driver.save_screenshot(screenshot)
            driver.close()
            time.sleep(3)
        else:
            driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]").click()
            time.sleep(2)

            # 点击确定
            driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").send_keys(d)
            time.sleep(1)

            # 重新登录用户账户
            driver.get("http://localhost:90/")
            # 输入用户名
            driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

            #  输入密码
            driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

            # 点击登陆
            driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
            time.sleep(1)

            # 点击个人中心
            driver.find_element(By.XPATH, "//i[@class='mdi mdi-account-location']").click()
            time.sleep(1)

            # 点击银行卡管理
            driver.find_element(By.LINK_TEXT, "银行卡管理").click()
            time.sleep(1)

            # 获取银行卡余额
            balanceweb = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
            time.sleep(2)

            #  获取实际标题结果
            balance = s.findBalanceByCardNum(e)

            # 判断银行卡余额是否为20000元
            # 修改数据库后会导致管理员无法正常收到网贷信息
            if str(balance) == "((Decimal('20000.00'),),)" and balanceweb == "账户余额：20000.00元":
                print("用例通过！")
            else:
                print("用例不通过！")

                screenshot = s.failedScreenshotNamed("test_002_addlyPass")
                driver.save_screenshot(screenshot)

            time.sleep(3)

            driver.close()

            time.sleep(1)

        self.assertEqual("账户余额：20000.00元", result)

    @unittest.skipIf(skip_test[2] == 1, "暂时跳过")
    @data(*da3)
    @unpack
    def test_003_addlyNoCard(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        userid = s.findIdByUserame(a)
        p = len(s.findBankcardById(userid))
        if p != 0:
            s.cleanCardById(userid)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(3)

        #  获取实际标题结果
        result = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text

        if "请先绑定银行卡" in result:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_003_addlyNoCard")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertIn("请先绑定银行卡", result)

    @unittest.skipIf(skip_test[3] == 1, "暂时跳过")
    @data(*da4)
    @unpack
    def test_004_addlyLarge(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(3)

        #  获取实际标题结果
        result = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text
        if "申请失败" in result:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_004_addlyLarge")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertIn("申请失败", result)

    @unittest.skipIf(skip_test[4] == 1, "暂时跳过")
    @data(*da5)
    @unpack
    def test_005_addlyLow(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(1)
        # 获取实际标题结果
        # 先判断有没有这个元素
        '''
        the_elms = driver.find_elements(By.XPATH, "//div[@id='jconfirm-content']/div")
        result = "未发现元素"
        if len(the_elms) != 0:
            result = driver.find_element(By.XPATH, "//div[@id='jconfirm-content']/div").text
        time.sleep(2)
        '''
        result = driver.find_element(By.XPATH,
                                     '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        # 判断银行卡名称是否在列表中
        if " 申请额度必须是100的整数倍" in result:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_005_addlyLow")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertIn("申请额度必须是100的整数倍", result)

    @unittest.skipIf(skip_test[5] == 1, "暂时跳过")
    @data(*da6)
    @unpack
    def test_006_addlyOverDate(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(1)
        '''
        #  获取实际标题结果
        the_elms = driver.find_elements(By.XPATH, "//div[@id='jconfirm-content']/div")
        result = "未发现元素"
        if len(the_elms) != 0:
            result = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text
        '''
        result = driver.find_element(By.XPATH,
                                     '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if "输入期限不合法" in result:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_006_addlyOverDate")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertIn("输入期限不合法", result)

    @unittest.skipIf(skip_test[6] == 1, "暂时跳过")
    @data(*da7)
    @unpack
    def test_007_addlyNoOverDate(self, a, b, c, d):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        p = len(s.findBankcardByNum(e))
        userid = s.findIdByUserame(a)
        if p == 0:
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        else:
            s.cleanCardByNum(e)
            s.addCompleteBankcard("中国银行", e, userid, 10000)
        s.setDefaultPayCard(userid, e)
        s.setReputationById(userid)
        s.setRealnameById('李四', userid)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常登录
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(a)

        #  输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(b)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, "//i[@class='mdi mdi-wrench']").click()
        time.sleep(1)

        # 点击安全网贷
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        time.sleep(1)

        # 输入借贷金额
        driver.find_element(By.XPATH, "//input[@id='amount']").send_keys(c)
        time.sleep(2)

        # 输入借贷限期
        driver.find_element(By.ID, "term").send_keys(d)
        time.sleep(1)

        # 点击申请
        driver.find_element(By.ID, "submit").click()
        time.sleep(2)

        # 点击确定
        driver.find_element(By.CSS_SELECTOR,
                            "body > div.jconfirm.jconfirm-light.jconfirm-open > div.jconfirm-scrollpane > div > div > div > div > div > div > div > div.jconfirm-buttons > button.btn.btn-primary").click()
        time.sleep(1)
        '''
        #  获取实际标题结果
        the_elms = driver.find_elements(By.XPATH, "//div[@id='jconfirm-content']/div")
        result = "未发现元素"
        if len(the_elms) != 0:
            result = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text
        '''
        result = driver.find_element(By.XPATH,
                                     '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

        if "申请成功" in result:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_007_addlyNoOverDate")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        driver.close()

        time.sleep(1)

        self.assertIn("申请成功", result)
